<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <style>
        :root{
            --squareLen: 400px
        }
        #grid{
            width: var(--squareLen);
            height: var(--squareLen);
            display: grid;
            outline: 2px solid gray;
            /* outline向外偏移4个像素*/
            outline-offset: 4px; 
            grid-template-columns: repeat(3, auto);
            grid-template-rows: repeat(3, auto);
        }
        .item{
            border: 1px solid palevioletred;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: -1px;
            margin-bottom: -1px;
        }
    </style>
    <button onclick="shuffle(event)" style="margin-bottom: 10px">乱序</button>
    <div id="grid">
        <div class="item">1</div>
        <div class="item">2</div>
        <div class="item">3</div>
        <div class="item">4</div>
        <div class="item">5</div>
        <div class="item">6</div>
        <div class="item">7</div>
        <div class="item">8</div>
        <div class="item">9</div>
    </div>
    <!-- <script src="./flip.js"></script> -->
    <script>
        function insertAfter(newNode, targetNode) {
            let parent = targetNode.parentNode;
            if(parent.lastElementChild === targetNode) {
                parent.appendChild(newNode)
            }else {
                parent.insertBefore(newNode, targetNode.nextElementSibling)
            }
        }
        // // flip动画
        function flipAni(selector) {
            const els = document.querySelectorAll(selector);
            const records = [...els].map(el => ({ el, rect: el.getBoundingClientRect() }))
            return {
                play() {
                    const curRecords = [...els].map(el => ({ el, rect: el.getBoundingClientRect() }));
                    const animations = [];
                    [...els].forEach((el, i)=> {
                        const invert1 = records[i].rect;
                        const invert2 = curRecords[i].rect;
                        // 返回一个Animation对象
                        const animation = el.animate([
                            { transform: `translate(${invert1.left - invert2.left}px, ${invert1.top - invert2.top}px)` },
                            { transform: 'none' }
                        ], {
                            duration: 1000,
                            fill: 'both',
                            easing: 'ease-in-out'
                        })
                        animations.push(animation)
                    });
                    // 动画结束后，一定要重新更新records
                    animations.forEach(async (animation, i) => {
                        await animation.finished;
                        records[i].rect = records[i].el.getBoundingClientRect();
                    })
                }
            }
        }
        const flip = flipAni('.item');
        // 乱序
        function shuffle() {
            const els = document.querySelectorAll('.item');
            const indexs = [];
            [...els].forEach((el, i)=> {
                // 生成[0, els.length)的随机数， https://www.runoob.com/jsref/jsref-random.html
                let index = Math.floor((Math.random() * els.length));
                // 防止随机数生成的索引值重复
                while(indexs.includes(index)) {
                    index = Math.floor((Math.random() * els.length));
                }
                indexs.push(index);
                const newIndex = indexs[i];
                if(newIndex != i) {
                    if(newIndex > i) { // 下移
                        insertAfter(els[newIndex], el)
                    }else { // 上移
                        el.parentNode.insertBefore(els[newIndex], el);
                    }
                }
            });
            flip.play();
        }
    </script>
</body>
</html>